package com.xayy.scheduled.service.impl;

import cn.kawins.mybatis.base.BaseServiceImpl;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Collection;

import com.xayy.scheduled.mapper.DataInfoMapper;
import com.xayy.scheduled.model.vo.DataInfoVo;
import com.xayy.scheduled.model.dto.DataInfoDto;
import com.xayy.scheduled.model.DataInfo;
import com.xayy.scheduled.service.DataInfoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import com.github.pagehelper.IPage;
import com.github.pagehelper.PageInfo;
import java.util.ArrayList;
import com.xayy.scheduled.convert.DataInfoConvert;
/**
* @Description: DataInfoService实现类
* @author liuyuan
* @date 2025-06-27
*/
@Service
@AllArgsConstructor
public class DataInfoServiceImpl extends BaseServiceImpl<DataInfo> implements DataInfoService {
        private final DataInfoMapper dataInfoMapper;

        @Override
        public PageInfo<DataInfoVo> page(DataInfoDto query) {
                List<DataInfo> list = this.pageList(getWrapper(query),query.getPageNum(),query.getPageSize());

                List<DataInfoVo> voList=DataInfoConvert.INSTANCE.convertToVOList(list);

                PageInfo<DataInfoVo> pageInfo=new PageInfo<DataInfoVo>(voList);
                pageInfo.setPageNum(query.getPageNum());
                pageInfo.setPageSize(query.getPageSize());
                return pageInfo;
        }

        private Example getWrapper(DataInfoDto query){
                Example example = new Example(DataInfo.class);
                if (query.getDataName()!=null) {
                   example.createCriteria().andEqualTo("dataName", query.getDataName());
                }
                if (query.getDataCode()!=null) {
                   example.createCriteria().andEqualTo("dataCode", query.getDataCode());
                }
                if (query.getHttpAddr()!=null) {
                   example.createCriteria().andEqualTo("httpAddr", query.getHttpAddr());
                }

                return example;
        }

        @Override
        public List<DataInfoVo> listByDTO(DataInfoDto query) {
                Example queryWrapper=getWrapper(query);
                List<DataInfo> entityList=this.selectByExample(queryWrapper);;
                return DataInfoConvert.INSTANCE.convertToVOList(entityList);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public DataInfo save(DataInfoVo vo) {
                DataInfo entity = DataInfoConvert.INSTANCE.convert(vo);
                DataInfo info = dataInfoMapper.selectByCode(vo.getDataCode());
                if (null == info){
                        dataInfoMapper.insert(entity);
                        return entity;
                }else {
                        return null;
                }
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public DataInfo getByCode(String code) {
                return dataInfoMapper.selectByCode(code);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public DataInfo update(DataInfoVo vo) {
                DataInfo entity = DataInfoConvert.INSTANCE.convert(vo);
                this.updateById(entity);
                return entity;
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public boolean saveBatch(Collection<DataInfo> entityList) {
                 return this.insertList(new ArrayList<>(entityList))>0;
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public void delete(List<Long> idList) {
                deleteByIds(idList);
        }

}